package com.servlet.dao;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import DB.DBManager;

import com.servlet.vo.bean.Account;

public class AccountDao {

	private Connection con;
	private Statement statement;
	private ResultSet resultSet;
	private boolean flag = false;
	private StringBuffer sql = new StringBuffer();
	private final String TABLE = "account";
	
	public AccountDao() throws SQLException{
		con = DBManager.getConn();
		statement = con.createStatement();
	}
	public void close(){
		
		if(null != resultSet){
			try {
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(null != statement){
			try {
				statement.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(null != con){
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public boolean save(Account obj) {
		sql.append("INSERT INTO " + TABLE + "(ADDTIME,deleteStatus,ac_name,availableBalance) VALUES( NOW(),FALSE,'");
		sql.append(obj.getAc_name() + "'," + BigDecimal.ZERO + ")");
		
		try {
			flag = statement.execute(sql.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			flag = true;
		}finally{
			close();
		}
		return flag;
	}
	
	public boolean delete(Long id){
		
		sql.append("UPDATE " + TABLE + " SET deleteStatus = TRUE WHERE id = '" + id + "'");
		try {
			flag = statement.execute(sql.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return flag;
	}
	
	public boolean update(Account obj , boolean refreshName){
		
		try {
			sql.append("UPDATE " + TABLE + " set ");
			if(obj.getAc_name() != null && !obj.getAc_name().equals("") && refreshName){
				sql.append("ac_name='" + obj.getAc_name() + "',");
			}
			sql.append("availableBalance=" + obj.getAvailableBalance());
			sql.append(" where id="+obj.getId());
					
			flag = statement.executeUpdate(sql.toString()) == 1;
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return flag;
	}
	
	public Account getObj(long id){
		Account obj = null;
		sql.append("SELECT id,addTime,deleteStatus,ac_name,availableBalance FROM " + TABLE );
		sql.append(" WHERE deleteStatus = FALSE and id = " + id);
		try {
			resultSet = statement.executeQuery(sql.toString());
			if(resultSet.next()){
				obj = new Account();
				obj.setId(resultSet.getLong("id"));
				obj.setAddTime(resultSet.getTimestamp("addTime"));
				obj.setDeleteStatus(resultSet.getBoolean("deleteStatus"));
				obj.setAc_name(resultSet.getString("ac_name"));
				obj.setAvailableBalance(resultSet.getBigDecimal("availableBalance"));
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return obj;
	}
	
	public List<Account> getList(){
		
		sql.append("SELECT id,addTime,ac_name,availableBalance FROM " + TABLE + " WHERE deleteStatus = FALSE ");
		List<Account> objs = new ArrayList<Account>();
		try {
			resultSet = statement.executeQuery(sql.toString());
			Account obj = null;
			while(resultSet.next()){
			
				obj = new Account();
				obj.setId(resultSet.getLong("id"));
				obj.setAddTime(resultSet.getTimestamp("addTime"));
				obj.setAc_name(resultSet.getString("ac_name"));
				obj.setAvailableBalance(resultSet.getBigDecimal("availableBalance"));
				objs.add(obj);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			close();
		}
		return objs;
	}
}